package com.doge.calcalbum.business.skin

import android.os.Bundle
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.setPadding
import androidx.recyclerview.widget.LinearLayoutManager
import com.doge.calcalbum.R
import com.doge.calcalbum.business.account.AccountHelper.self
import com.doge.calcalbum.business.vip.VipHelper
import com.doge.calcalbum.core.BaseFragment
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.databinding.FragmentSkinBinding
import com.doge.calcalbum.utils.getColor
import com.doge.calcalbum.utils.systemWindowInsetsTop
import com.doge.calcalbum.widgets.AlertDialog
import com.doge.calcalbum.widgets.Spanny

class SkinFragment : BaseFragment() {
    private var innerBinding: FragmentSkinBinding? = null
    private val binding get() = innerBinding!!
    private val adapter by lazy { SkinAdapter() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding = innerBinding ?: FragmentSkinBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        systemWindowInsetsTop.observe(viewLifecycleOwner) {
            binding.fitsSys.setPadding(it / 2)
        }
        binding.recyclerView.itemAnimator = null
        binding.recyclerView.layoutManager = LinearLayoutManager(binding.recyclerView.context)
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        adapter.submitList(SkinHelper.skinList)
    }

    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            ITEM_CLICKED -> showSkinSetupDialog(item as? Skin?)
        }
    }

    private fun showSkinSetupDialog(data: Skin?) {
        if (data == null) return
        val context = activity ?: return
//        if (self.canGetFreeVip == 1) {
//            VipHelper.showGetFreeVipDialog()
//            return
//        }
        // vipCheck
        if(data.type == Skin.TYPE_SKIN && !self.isVip) {
            VipHelper.showBuyDialog("skin", context) // vipCheck
            return
        }

        AlertDialog.Builder(context)
            .setTitle(getString(R.string.skin_use_title))
            .setMessage(Spanny().apply {
                append(getString(R.string.skin_use_desc))
                append(getString(R.string.skin_use_desc_tip), ForegroundColorSpan(getColor("#F56243")))
            })
            .setNegativeButton(getString(R.string.cancel1)) { _, _ -> }
            .setPositiveButton(getString(R.string.confirm)) { _, _ -> SkinHelper.setup(data.id) }
            .create().show()
    }

    override fun onResume() {
        super.onResume()
        adapter.notifyDataSetChanged()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        adapter.setListener(null)
        innerBinding = null
    }
}